package com.xdf.docmanagementsystem.controller;

import com.xdf.docmanagementsystem.common.base.BaseController;
import com.xdf.docmanagementsystem.enums.ResponseEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.xdf.docmanagementsystem.entity.ResponseResult;

import java.util.*;
import java.util.stream.Collectors;

import com.xdf.docmanagementsystem.entity.UserDeploy;
import com.xdf.docmanagementsystem.service.UserDeployService;

/**
 * @Description : 自动生成的控制层
 * @Author : ZK
 * @Date : 2020-06-19 18:48:28
 */
@Api(tags = "用户网格配置")
@CrossOrigin
@RestController
@RequiresUser
@RequestMapping("/userDeploy")
public class UserDeployController extends BaseController {

    @Autowired
    private UserDeployService service;

    /**
     * @Description : 选择新增
     * @Return : ResponseResult<Void>
     * @Author : ZK
     * @Date : 2020-06-19 18:48:28
     */
    @ApiOperation(value = "选择新增", httpMethod = "POST", notes = "自动生成的新增方法")
    @RequestMapping(value = "/insertSelective", method = RequestMethod.POST)
    public ResponseResult<Void> insertSelective(UserDeploy record) throws Exception {
        service.insertSelective(record);
        return new ResponseResult<Void>(true, ResponseEnum.OK.getMessage());
    }

    /**
     * @Description : 批量新增
     * @Return : ResponseResult<Void>
     * @Author : ZK
     * @Date : 2020-06-19 18:48:28
     */
    @ApiOperation(value = "批量新增", httpMethod = "POST", notes = "自动生成的新增方法")
    @RequestMapping(value = "/multiInsert", method = RequestMethod.POST)
    public ResponseResult<Void> multiInsert(@RequestBody List<UserDeploy> list) throws Exception {
        service.multiInsert(list);
        return new ResponseResult<Void>(true, ResponseEnum.OK.getMessage());
    }

    /**
     * @Description : 选择修改
     * @Return : UserDeploy
     * @Author : ZK
     * @Date : 2020-06-19 18:48:28
     */
    @ApiOperation(value = "选择修改", httpMethod = "PUT", notes = "自动生成的修改方法")
    @RequestMapping(value = "/updateSelective", method = RequestMethod.POST)
    public ResponseResult<Void> updateSelective(UserDeploy record) throws Exception {
        service.updateByPrimaryKeySelective(record);
        return new ResponseResult<Void>(true, ResponseEnum.OK.getMessage());
    }

    /**
     * @Description : 批量修改
     * @Return : UserDeploy
     * @Author : ZK
     * @Date : 2020-06-19 18:48:28
     */
    @ApiOperation(value = "批量修改", httpMethod = "POST", notes = "自动生成的修改方法")
    @RequestMapping(value = "/multiUpdate", method = RequestMethod.POST)
    public ResponseResult<Void> multiUpdate(@RequestBody List<UserDeploy> list) throws Exception {
        service.multiUpdate(list);
        return new ResponseResult<Void>(true, ResponseEnum.OK.getMessage());
    }

    /**
     * @Description : 根据主键查询
     * @Return : UserDeploy
     * @Author : ZK
     * @Date : 2020-06-19 18:48:28
     */
    @ApiOperation(value = "根据主键查询", httpMethod = "GET", notes = "自动生成的查询方法")
    @RequestMapping(value = "/queryByPk/{id}", method = RequestMethod.GET)
    public UserDeploy selectByPrimaryKey(@PathVariable("id") Integer id) throws Exception {
        if (id == null) return null;
        return service.selectByPrimaryKey(id);
    }

    /**
     * @Description : 查询方法
     * @Return : java.util.List
     * @Author : ZK
     * @Date : 2020-06-19 18:48:28
     */
    @ApiOperation(value = "查询方法", httpMethod = "GET", notes = "自动生成的查询方法")
    @RequestMapping(value = "/listQuery", method = RequestMethod.GET)
    public List<UserDeploy> query(UserDeploy record) throws Exception {
        return service.listQuery(record);
    }

    /**
     * @Description : 根据主键删除方法
     * @Return : ResponseResult<Void>
     * @Author : ZK
     * @Date : 2020-06-19 18:48:28
     */
    @ApiOperation(value = "根据主键删除方法", httpMethod = "DELETE", notes = "自动生成的删除方法")
    @RequestMapping(value = "/deleteByPk/{id}", method = RequestMethod.POST)
    public ResponseResult<Void> deleteByPrimaryKey(@PathVariable("id") Integer id) throws Exception {
        service.deleteByPrimaryKey(id);
        return new ResponseResult<Void>(true, ResponseEnum.OK.getMessage());
    }

    /**
     * 根据菜单名查询
     *
     * @param list
     * @return
     * @throws Exception
     */
    @PostMapping("/query/batch")
    public Map<String, UserDeploy> listQueryByParam(@RequestBody List<UserDeploy> list) throws Exception {
        List<UserDeploy> answer = service.listQuery(list);
        return Optional.ofNullable(answer)
                .orElse(new ArrayList<>())
                .stream()
                .filter(x -> !StringUtils.isEmpty(x.getField()))
                .collect(Collectors.toMap(x -> x.getField(), x -> x, (x, y) -> x));
    }

    /**
     * 修改排序字段
     *
     * @param list
     * @return
     */
    @PostMapping("/update/batch")
    public ResponseResult<Void> updateSerialNumber(@RequestBody List<UserDeploy> list) throws Exception {
        service.multiUpdate(list);
        return new ResponseResult<Void>(true, ResponseEnum.OK.getMessage());
    }

    /**
     * @param userDeploy
     * @return
     * @throws Exception
     */
    @PostMapping("/update/single")
    public ResponseResult<Void> updateSingle(UserDeploy userDeploy) throws Exception {
        service.updateByPrimaryKeySelective(userDeploy);
        return new ResponseResult<Void>(true, ResponseEnum.OK.getMessage());
    }

    /**
     * 重置网格配置
     *
     * @param menuId
     * @return
     */
    @GetMapping("/reset")
    public ResponseResult<List<UserDeploy>> resetGridSetting(Integer menuId) throws Exception {
        service.resetDeploy(menuId);
        List<UserDeploy> list = service.listQuery(UserDeploy.builder().menuId(menuId).userId(getUserId()).build());
        return new ResponseResult<List<UserDeploy>>(true, ResponseEnum.OK.getState(), ResponseEnum.OK.getMessage(), list);
    }
}